package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.pageprocessor.amac.DisciplinaryActionProcessor;
import tech.abing.spider.task.pipeline.amac.DisciplinaryActionPipeline;
import tech.abing.spider.util.SqlUtil;

/**
 * 抓取纪律处分
 * @author Ivan qb.huang@wescxx.com   
 * @date 2016-10-31 下午02:53:59 
 * @version V2.0
 */
public class DisciplinaryActionSpider {
	
	private static SqlUtil sqlUtil = null;

	/**
	 * @param args
	 */
	public static void start() {
		
		CrawlManager manager = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = manager
		.prepareCrawlTask(DictEnum.TASK_TYPE_DISCIPLINARY_ACTION.getName(), DefaultPageDownloader.class)
		.useThread(1)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(1000)
		.usePipeline(DisciplinaryActionPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_DISCIPLINARY_ACTION.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_ATTACHMENT.getCode()+" where 1=1 and ATTACH_TYPE = ?", TaskConstants.TASK_DISCIPLINARY_ACTION);
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		manager.start(task);

	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			
			StartContext context = new StartContext();
			
			String url = "http://www.amac.org.cn/xxgs/jlcf/";
			
			PageRequest req = context.createPageRequest(url, DisciplinaryActionProcessor.class);
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
			req.putHeader("Referer", "http://www.amac.org.cn/xxgs/hmd/");
			req.putHeader("Host", "www.amac.org.cn");
			req.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control", "no-cache");
			req.putHeader("Upgrade-Insecure-Requests", "1");
			req.putHeader("Cookie", "BIGipServerPool_Web=1326711050.20480.0000; 22_vq=53");
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}
		
	}

}
